package com.klikmee.sms.core.base.communicator.starion;

import java.io.IOException;
import java.text.MessageFormat;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.HttpRequestHandler;

/**
 * This class is responsible for handling Starion's http delivery report requests.
 * Delivery report parameters are extracted from request and forwarded
 * to {@link StarionCommunicator} implementation for further processing. 
 * @author makcro
 *
 */
public class DeliveryReportHandlerImpl implements HttpRequestHandler {

	/**
	 * Logger instance
	 */
	private Logger logger = Logger.getLogger(DeliveryReportHandlerImpl.class);
	
	/**
	 * Starion communicator instance
	 */
	private StarionCommunicator communicator = null;

	/**
	 * Enumeration of starions delivery report params
	 * @author makcro
	 *
	 */
	public enum StarionDeliveryReportParams {
		
		MSG_ID("msg_id"), CHARGED("charged");
		
		private StarionDeliveryReportParams(String name){
			this.name = name;
		}
		
		private String name;
		
		public String getName() {
			return name;
		}
	}
	
	/**
	 *  Called when new delivery report request is received.
	 *  Extracts delivery report params from request and forwards it to
	 *  {@link StarionCommunicator} instance for further processing
	 *  
	 */
	
	@Override
	public void handleRequest( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
		
		String startionMessageId = request.getParameter(StarionDeliveryReportParams.MSG_ID.getName());
		String charged = request.getParameter(StarionDeliveryReportParams.CHARGED.getName());

		this.communicator.onDeliveryReport(startionMessageId, charged);
	}
	
	public void setCommunicator(StarionCommunicator communicator) {
		this.communicator = communicator;
	}

}
